package com.wskj.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wskj.common.core.mybatisplus.core.ServicePlusImpl;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.utils.common.AssertUtils;
import com.wskj.common.utils.common.PageUtils;
import com.wskj.common.utils.common.SecurityUtils;
import com.wskj.common.utils.tool.Condition;
import com.wskj.common.utils.tool.Query;
import com.wskj.system.domain.SysNotice;
import com.wskj.system.domain.SysNoticeRead;
import com.wskj.system.mapper.SysNoticeMapper;
import com.wskj.system.service.ISysNoticeReadService;
import com.wskj.system.service.ISysNoticeService;
import com.wskj.system.vo.app.AppSysNoticeVo;
import com.wskj.system.vo.app.AppUserNoticeVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
@AllArgsConstructor
public class SysNoticeServiceImpl extends ServicePlusImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    private final ISysNoticeReadService iSysNoticeReadService;

    @Override
    public TableDataInfo<SysNotice> selectPageNoticeList(SysNotice notice) {
        LambdaQueryWrapper<SysNotice> lqw = new LambdaQueryWrapper<SysNotice>()
                .eq(StrUtil.isNotBlank(notice.getStatus()),SysNotice::getStatus,notice.getStatus())
                .like(StrUtil.isNotBlank(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle())
                .eq(StrUtil.isNotBlank(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType())
                .like(StrUtil.isNotBlank(notice.getCreateBy()), SysNotice::getCreateBy, notice.getCreateBy())
                .orderByDesc(SysNotice::getReleaseTime)
                .orderByDesc(SysNotice::getCreateTime);
        return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw));
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return getById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return list(new LambdaQueryWrapper<SysNotice>()
                .like(StrUtil.isNotBlank(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle())
                .eq(StrUtil.isNotBlank(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType())
                .like(StrUtil.isNotBlank(notice.getCreateBy()), SysNotice::getCreateBy, notice.getCreateBy()));
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        return baseMapper.insert(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return baseMapper.updateById(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return baseMapper.deleteById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(noticeIds));
    }

    @Override
    public TableDataInfo<AppSysNoticeVo> systemList(Query query) {
        IPage<AppSysNoticeVo> iPage = baseMapper.systemList(Condition.getPage(query));
        return PageUtils.buildDataInfo(iPage);
    }

    @Override
    public TableDataInfo<AppUserNoticeVo> userList(Query query) {
        Long userId = SecurityUtils.getUserId();
        IPage<AppUserNoticeVo> iPage = baseMapper.userList(Condition.getPage(query),userId);
        return PageUtils.buildDataInfo(iPage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int read(Long noticeId) {
        Long userId = SecurityUtils.getUserId();
        SysNotice dbSysNotice = baseMapper.selectById(noticeId);
        AssertUtils.notNull(dbSysNotice,"该消息已不存在");
        LambdaQueryWrapper<SysNoticeRead> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysNoticeRead::getNoticeId,noticeId);
        lambdaQueryWrapper.eq(SysNoticeRead::getUserId,userId);
        long existCount  = iSysNoticeReadService.count(lambdaQueryWrapper);
        if(existCount>0){
            return 1;
        }
        SysNoticeRead sysNoticeRead = new SysNoticeRead();
        sysNoticeRead.setNoticeId(noticeId);
        sysNoticeRead.setUserId(userId);
        sysNoticeRead.setReadTime(new Date());
        return iSysNoticeReadService.getBaseMapper().insert(sysNoticeRead);
    }

    @Override
    public int waitReadCount() {
        Long userId = SecurityUtils.getUserId();
        return baseMapper.waitReadCount(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int del(Long noticeId) {
        Long userId = SecurityUtils.getUserId();
        SysNotice dbSysNotice = baseMapper.selectById(noticeId);
        AssertUtils.notNull(dbSysNotice, "该消息已被删除");
        /*if (NoticeTypeEnum.NOTICE.getValue().equals(dbSysNotice.getNoticeType())) {
            LambdaQueryWrapper<SysNoticeDel> sysNoticeDelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysNoticeDelLambdaQueryWrapper.eq(SysNoticeDel::getNoticeId, noticeId);
            sysNoticeDelLambdaQueryWrapper.eq(SysNoticeDel::getUserId, userId);
            int count = sysNoticeDelMapper.selectCount(sysNoticeDelLambdaQueryWrapper);
            if (count == 0) {
                SysNoticeDel sysNoticeDel = new SysNoticeDel();
                sysNoticeDel.setNoticeId(noticeId);
                sysNoticeDel.setUserId(userId);
                sysNoticeDel.setDelTime(new Date());
                sysNoticeDelMapper.insert(sysNoticeDel);
            }
        } else {
            baseMapper.deleteById(noticeId);
        }*/
        return 1;
    }
}
